package club.xiaojiawei.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 5/20/22 8:36 PM
 * @question 17. 电话号码的字母组合
 * @description 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 */
public class LetterCombinations17 {

    /**
     * 基准数47,然后加上字符数字[47 + i, 49 + i]
     * @param args
     */
    public static void main(String[] args) {
        LetterCombinations17 test = new LetterCombinations17();
        List<String> result = test.letterCombinations("23");
        System.out.println(result.size());
        System.out.println(result);
    }

    /**
     * 递归
     * @param digits
     * @return
     */
    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0){
            return result;
        }
        this.temp = new char[digits.length()];
        this.table = new char[][]{{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};
        recursion(0, digits);
        return result;
    }

    char[] temp;

    int count = 0;

    List<String> result = new ArrayList<>();

    char[][] table = null;

    public void recursion(int index, String digits){
        char[] chars = table[digits.charAt(index) - 50];
        for (char c : chars) {
            temp[count++] = c;
            if (count == digits.length()){
                result.add(new String(temp));
            }else {
                recursion(index + 1, digits);
            }
            count--;
        }
    }

    /**
     * 官方-回溯
     * @param digits
     * @return
     */
    public List<String> letterCombinations2(String digits) {
        List<String> combinations = new ArrayList<String>();
        if (digits.length() == 0) {
            return combinations;
        }
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        return combinations;
    }

    public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
        if (index == digits.length()) {
            combinations.add(combination.toString());
        } else {
            char digit = digits.charAt(index);
            String letters = phoneMap.get(digit);
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
                combination.append(letters.charAt(i));
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                combination.deleteCharAt(index);
            }
        }
    }
}
